<?php
namespace app\know\controller;

use app\know\controller\AdminKnowBaseController;
use cmf\lib\Upload;

/**
 * Class AdminUploadController 上传控制器
 * @package app\know\controller
 */
class AdminUploadController extends AdminKnowBaseController
{
    
	public function upload()
    {
        if (empty($this->request->isPost())) 
        {
            $this->error('请求方法不支持');
        }

        $uploader = new Upload();
        $fileType = $this->request->param('filetype','image');
        $uploader->setFileType($fileType);

        $result = $uploader->upload();

        if ($result === false) 
        {
            $this->error($uploader->getError());
        } 
        else
        {

            $result['preview_url'] = $fileType === 'image' ? cmf_get_image_preview_url($result["filepath"]) : cmf_get_asset_url($result["filepath"]);
            $result['url']         = $fileType === 'image' ? cmf_get_image_url($result["filepath"]) : cmf_get_file_download_url($result["filepath"]);
            $result['filename']    = $result["name"];
            $result['asset_id']    = $result["asset_id"];
            $this->success('上传成功', null, $result);
        }
    }
	
	
}
